package com.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.demo.constant.Constant;
import com.demo.entity.MessageRule;
import com.demo.mapper.MedicineMapper;
import com.demo.mapper.MessageRuleMapper;
import com.demo.response.Result;
import com.demo.service.IMessageRuleService;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

/**
 * 药物提醒规则配置表 服务实现类
 *
 * @author ZM
 * @since 2020-11-23
 */
@Service
public class MessageRuleServiceImpl extends ServiceImpl<MessageRuleMapper, MessageRule> implements IMessageRuleService {
    @Resource
    MessageRuleMapper ruleMapper;
    @Resource
    private MedicineMapper medicineMapper;
    @Resource
    private MessageRuleMapper messageRuleMapper;

    /**
     * 新增药物提醒规则
     *
     * @param messageRule 实体对象
     * @return
     */
    @Override
    public Result addMessageRule(MessageRule messageRule) {
        messageRule.setUpdatedUserId(messageRule.getCreatedUserId());
        save(messageRule);
        return Result.success();
    }

    /**
     * 更新药物提醒规则
     *
     * @param messageRule 实体对象
     * @return
     */
    @Override
    public Result updateMessageRule(MessageRule messageRule) {
        if (messageRule.getMessageRuleId() == null || messageRule.getUpdatedUserId() == null) {
            return Result.error(Constant.KEY_ID);
        }
        messageRule.setUpdatedTime(new Date());
        updateById(messageRule);
        return Result.success();
    }

    /**
     * 删除药物提醒规则
     *
     * @param messageRuleId 规则ID
     * @param deletedUserId 删除人ID
     * @return
     */
    @Override
    public Result deleteMessageRule(Integer messageRuleId, Integer deletedUserId) {
        MessageRule rule = new MessageRule();
        rule.setMessageRuleId(messageRuleId);
        rule.setDeletedUserId(deletedUserId);
        rule.setDeletedTime(new Date());
        updateById(rule);
        return Result.success();
    }

    /**
     * 药物提醒规则列表
     *
     * @param pageNumber 页码
     * @param pageSize   每页数量
     * @param medicineId 药物ID
     * @return
     */
    @Override
    public Result messageRuleList(Integer pageNumber, Integer pageSize, Integer medicineId) {
        IPage<MessageRule> page = new Page<>(pageNumber, pageSize);
        QueryWrapper<MessageRule> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(medicineId != null, "medicine_id", medicineId);
        queryWrapper.isNull("deleted_user_id");
        queryWrapper.orderByAsc("item_sort");
        page = ruleMapper.selectPage(page, queryWrapper);
        PageInfo<MessageRule> pageInfo = new PageInfo<>(page.getRecords());
        pageInfo.setPages((int) page.getPages());
        pageInfo.setTotal(page.getTotal());
        if (pageNumber.equals(page.getPages())) {
            pageInfo.setIsLastPage(false);
        } else {
            pageInfo.setIsLastPage(true);
        }
        return Result.success(pageInfo);
    }
}
